package net.wanho.controller;

import io.lettuce.core.dynamic.annotation.Param;
import lombok.RequiredArgsConstructor;
import net.wanho.constant.CommonConstant;
import net.wanho.dto.ProductDTO;
import net.wanho.dto.TransshipmentDTO;
import net.wanho.po.Transshipment;
import net.wanho.service.BrandService;
import net.wanho.service.ProductService;
import net.wanho.service.StoreService;
import net.wanho.service.TransshipmentService;
import net.wanho.util.CurrentUtil;
import net.wanho.vo.PageInfo;
import net.wanho.vo.ProductVO;
import net.wanho.vo.R;
import net.wanho.vo.TransshipmentVo;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequiredArgsConstructor
@RequestMapping("/transshipment")
public class TransshipmentController {
    private final TransshipmentService transshipmentService;
    private final StoreService storeService;
    private final ProductService productService;
    private final BrandService brandService;
    @GetMapping("/store-list")
    public R storeList(){
       return R.success(storeService.list());
    }
    @GetMapping("/transshipment-page-list")
    public R transshipmentPageList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize, TransshipmentDTO transshipment){
        PageInfo<TransshipmentVo> pageInfo = transshipmentService.transshipmentPageList(pageNum, pageSize, transshipment);
        return R.success(pageInfo);

    }
    @GetMapping("/product-page-list")
    public R productPageList(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize, ProductDTO productDTO){
        PageInfo<ProductVO> productList = productService.productPageList(pageNum,pageSize,productDTO);
        return R.success(productList);
    }

    @GetMapping("/brand-list")
    public R brandList(){
        return R.success(brandService.list());
    }

    @GetMapping("/product-num-check")
    public R productNumCheck(@Param("productNum") String productNum){
        return R.success(productService.productNumCheck(productNum));
    }

    @PostMapping("/transshipment-add")
    public R transshipmentAdd(@RequestBody TransshipmentDTO transshipmentDTO){
        transshipmentDTO.setCreateBy(CurrentUtil.getUserId());
        transshipmentDTO.setCreateTime(new Date());
        transshipmentDTO.setAuditState(CommonConstant.AUDIT_STATE_0);
        transshipmentService.transshipmentAdd(transshipmentDTO);
        return R.success("调货成功");
    }

    @PutMapping("/transshipment-confirm")
    public R transshipmentConfirm(@RequestBody Transshipment transshipment){
        transshipment.setAuditState(CommonConstant.AUDIT_STATE_1);
        transshipmentService.updateById(transshipment);
        return R.success("确认调货");
    }


}
